package com.seepln.connect.impl;
/**
 * Created by
 *
 * @auth: liqy
 * on 2021/4/20 16:04.
 * desc:
 */

import cn.hutool.core.util.ClassLoaderUtil;
import com.seepln.manage.ConnComponents;

import java.io.File;
import java.io.IOException;

/**
 * @ClassName: MySQLDriverFactory
 * @Description: TODO
 * @Author: liqy
 * @CreateDate: 2021/4/20 16:04
 * @UpdateUser: liqy
 * @UpdateDate: 2021/4/20  16:04
 * @UpdateRemark: update TODO
 * @Version: 1.0
 */
public class DBClassLoaderFactory {

    String libPath;

    public DBClassLoaderFactory(String libPath) {
        this.libPath = libPath;
        String path = getPath();
        try {
            ConnComponents.geneConnComponents(path, libPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ClassLoader getDbClassLoader(String dbType, String version) {
        String dbJarPre = "mysql-connector-java-";
        switch (dbType.toLowerCase()) {
            case "mysql":
                break;
            case "oracle":
                dbJarPre = "ojdbc6-";
                break;
            case "postgre":
                dbJarPre = "postgresql-";
                break;
            case "sqlserver":
                dbJarPre = "mssql-jdbc-";
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + dbType);
        }
        String versionPath = libPath + "/" + dbJarPre + version + ".jar";
        return ClassLoaderUtil.getJarClassLoader(new File(versionPath));
    }

    public String getPath() {
        String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        if (System.getProperty("os.name").contains("dows")) {
            path = path.substring(1, path.length());
        }
        if (path.contains("jar")) {
            return path;
        }
        return path.replace("target/classes/", "");
    }
}
